/**
 * Copyright (C) 2016 Robinhood Markets, Inc.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.github.jinatonic.confetti;

import com.github.jinatonic.confetti.animation.Interpolator;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.Path;
import ohos.agp.render.Texture;
import ohos.agp.utils.Color;
import ohos.global.resource.Resource;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Size;

import java.util.ArrayList;
import java.util.List;

public class Utils {
    private static final Paint PAINT = new Paint();

    static {
        PAINT.setStyle(Paint.Style.FILL_STYLE);
    }

    private static Interpolator defaultAlphaInterpolator;

    public static Interpolator getDefaultAlphaInterpolator() {
        if (defaultAlphaInterpolator == null) {
            defaultAlphaInterpolator = new Interpolator() {
                @Override
                public float getInterpolation(float v) {
                    return v >= 0.9f ? 1f - (v - 0.9f) * 10f : 1f;
                }
            };
        }
        return defaultAlphaInterpolator;
    }

    public static List<PixelMap> generateConfettiBitmaps(int[] colors, int size) {
        List<PixelMap> bitmaps = new ArrayList<>();
        for (int color : colors) {
            bitmaps.add(createCircleBitmap(color, size));
            bitmaps.add(createSquareBitmap(color, size));
            bitmaps.add(createTriangleBitmap(color, size));
        }
        return bitmaps;
    }

    public static PixelMap createCircleBitmap(int color, int size) {
        PixelMap bitmap = createPixelMap(size);
        Canvas canvas = new Canvas(new Texture(bitmap));
        PAINT.setColor(new Color(color));

        float radius = size / 2f;
        canvas.drawCircle(radius, radius, radius, PAINT);
        return bitmap;
    }

    public static PixelMap createSquareBitmap(int color, int size) {
        PixelMap bitmap = createPixelMap(size);
        Canvas canvas = new Canvas(new Texture(bitmap));
        PAINT.setColor(new Color(color));

        Path path = new Path();
        path.moveTo(0, 0);
        path.lineTo(size, 0);
        path.lineTo(size, size);
        path.lineTo(0, size);
        path.close();

        canvas.drawPath(path, PAINT);
        return bitmap;
    }

    public static PixelMap createTriangleBitmap(int color, int size) {
        PixelMap bitmap = createPixelMap(size);
        Canvas canvas = new Canvas(new Texture(bitmap));
        PAINT.setColor(new Color(color));

        Path path = new Path();
        float point = (float) Math.tan(15f / 180f * Math.PI) * size;
        path.moveTo(0, 0);
        path.lineTo(size, point);
        path.lineTo(point, size);
        path.close();

        canvas.drawPath(path, PAINT);
        return bitmap;
    }

    private static PixelMap createPixelMap(int size) {
        PixelMap.InitializationOptions initializationOptions = new PixelMap.InitializationOptions();
        initializationOptions.size = new Size(size, size);
        initializationOptions.pixelFormat = PixelFormat.ARGB_8888;
        initializationOptions.editable = true;
        return PixelMap.create(initializationOptions);
    }
}
